<template>
  <el-container class="layout-container">
    <el-aside :width="isCollapse ? '64px' : '200px'">
      <div class="logo">
        <h1 v-show="!isCollapse">权限管理系统</h1>
        <h1 v-show="isCollapse">权</h1>
      </div>
      <el-menu
        :default-active="activeMenu"
        class="el-menu-vertical"
        :router="true"
        background-color="#304156"
        text-color="#fff"
        active-text-color="#409EFF"
        :collapse="isCollapse"
      >
        <el-menu-item index="/dashboard">
          <el-icon><Monitor /></el-icon>
          <template #title>首页</template>
        </el-menu-item>
        <el-menu-item index="/users">
          <el-icon><User /></el-icon>
          <template #title>用户管理</template>
        </el-menu-item>
        <el-menu-item index="/permissions">
          <el-icon><Setting /></el-icon>
          <template #title>权限管理</template>
        </el-menu-item>
      </el-menu>
    </el-aside>
    
    <el-container>
      <el-header>
        <div class="header-content">
          <div class="header-left">
            <el-icon 
              class="collapse-icon"
              @click="toggleCollapse"
            >
              <Fold v-if="!isCollapse"/>
              <Expand v-else/>
            </el-icon>
            <div class="breadcrumb">
              <el-breadcrumb>
                <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>{{ currentRoute }}</el-breadcrumb-item>
              </el-breadcrumb>
            </div>
          </div>
          <div class="header-right">
            <el-dropdown @command="handleCommand" trigger="hover">
              <div class="user-info">
                <el-avatar :size="32" :src="avatarUrl" class="user-avatar">管</el-avatar>
                <span class="username">管理员</span>
                <el-icon class="el-icon--right"><arrow-down /></el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">
                    <el-icon><User /></el-icon>
                    <span>个人信息</span>
                  </el-dropdown-item>
                  <el-dropdown-item command="settings">
                    <el-icon><Setting /></el-icon>
                    <span>系统设置</span>
                  </el-dropdown-item>
                  <el-dropdown-item divided command="logout">
                    <el-icon><SwitchButton /></el-icon>
                    <span>退出登录</span>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>
      
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Monitor, User, Setting, ArrowDown, SwitchButton, Fold, Expand } from '@element-plus/icons-vue'
import { useUserStore } from '../stores/user'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const isCollapse = ref(false)

const avatarUrl = ref('')
const activeMenu = computed(() => route.path)

const currentRoute = computed(() => {
  const routeMap = {
    '/dashboard': '仪表板',
    '/users': '用户管理',
    '/permissions': '权限管理'
  }
  return routeMap[route.path] || ''
})

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const handleCommand = (command) => {
  if (command === 'logout') {
    userStore.logout()
    router.push('/login')
  } else if (command === 'profile') {
    // 处理个人信息
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #2b3a4d;
  overflow: hidden;
  transition: all 0.3s;
}

.logo h1 {
  color: #fff;
  font-size: 18px;
  margin: 0;
  font-weight: 600;
  white-space: nowrap;
}

.el-header {
  background-color: #fff;
  border-bottom: 1px solid #dcdfe6;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
}

.header-content {
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.collapse-icon {
  font-size: 20px;
  cursor: pointer;
  color: #606266;
  transition: all 0.3s;
}

.collapse-icon:hover {
  color: #409EFF;
}

.header-right {
  display: flex;
  align-items: center;
}

.el-aside {
  background-color: #304156;
  transition: width 0.3s;
  overflow: hidden;
}

.el-menu {
  border-right: none;
}

.el-menu-vertical {
  height: calc(100vh - 60px);
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 200px;
}

:deep(.el-menu--collapse) {
  width: 64px;
}

:deep(.el-menu-item) {
  &.is-active {
    background-color: #263445 !important;
  }
  
  &:hover {
    background-color: #263445 !important;
  }
}

.breadcrumb {
  display: flex;
  align-items: center;
}

/* 添加用户头像和下拉菜单的样式 */
.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  flex-shrink: 0;
  background: linear-gradient(135deg, #36d1dc 0%, #5b86e5 100%);
  border: 2px solid transparent;
  transition: all 0.3s ease;
  color: #fff;
  font-weight: 500;
}

.user-info:hover .user-avatar {
  border-color: #5b86e5;
  box-shadow: 0 2px 8px rgba(91, 134, 229, 0.2);
}

.el-dropdown {
  height: 100%;
  display: flex;
  align-items: center;
}

:deep(.el-dropdown-menu) {
  min-width: 120px !important;
}

:deep(.el-dropdown-menu__item) {
  padding: 8px 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

:deep(.el-dropdown__popper) {
  margin-top: 5px !important;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 4px 12px;
  border-radius: 4px;
  transition: all 0.3s;
}

.user-info:hover {
  background-color: #f6f6f6;
}

.username {
  margin-left: 8px;
}

.el-icon--right {
  color: #909399;
  transition: transform 0.3s;
  display: inline-block !important;
  font-size: 12px;
  margin-left: 4px;
}

:deep(.el-dropdown-menu) {
  padding: 4px 0;
  margin-top: 4px !important;
  min-width: 120px !important;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
}

:deep(.el-dropdown:hover .el-icon--right) {
  transform: rotate(180deg);
}

:deep(.el-dropdown-menu__item) {
  padding: 8px 16px;
  display: flex;
  align-items: center;
  font-size: 14px;
  line-height: 1.5;
  color: #606266;
  transition: all 0.3s;
}

:deep(.el-dropdown-menu__item .el-icon) {
  margin-right: 10px;
  font-size: 16px;
}

:deep(.el-dropdown-menu__item:hover) {
  background-color: #f5f7fa;
  color: #409EFF;
}

:deep(.el-dropdown-menu__item--divided) {
  position: relative;
  margin-top: 6px;
  padding-top: 12px;
}

:deep(.el-dropdown-menu__item--divided::before) {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background-color: #ebeef5;
}
</style> 